<template>
  <el-card>
    <el-button
      :icon="Plus"
      class="mb-20"
      type="primary"
      @click="addSPU"
      :disabled="!categoryStore.category3Id"
      >添加SPU
    </el-button>
    <el-table class="mb-20" :data="spuList" border>
      <el-table-column
        width="80"
        label="序号"
        type="index"
        align="center"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            @click="addSku(row)"
            type="primary"
            :icon="Plus"
            size="small"
            title="添加SKU"
          />
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            title="修改SPU"
            @click="updateSpu(row)"
          />
          <el-button
            type="info"
            size="small"
            :icon="InfoFilled"
            title="查看SKU列表"
          />
          <el-button
            size="small"
            type="danger"
            :icon="Delete"
            title="删除SPU"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :page-sizes="[5, 10, 15, 20]"
      v-model:page-size="pageSize"
      v-model:current-page="currentPage"
      @size-change="getProductSPUList"
      @current-change="getProductSPUList"
      layout=" prev, pager, next, jumper, ->, sizes, total "
    ></el-pagination>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "spuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Delete, Edit, InfoFilled, Plus } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { getProductSPUListApi } from "@/api/product/spu";
import { useSpuStore } from "@/stores/spu";
import type {
  GetProductSPUList,
  GetProductSPUItem,
} from "@/api/product/model/spuModel";
const categoryStore = useCategoryStore();
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const spuList = ref<GetProductSPUList>([]);
const spuStore = useSpuStore();
const emit = defineEmits(["setIsShow"]);
// 切换到添加/Sku组件
const addSku = (row: GetProductSPUItem) => {
  emit("setIsShow", 3);
  spuStore.spu = row;
};

// 切换到添加/编辑页面
const addSPU = () => {
  emit("setIsShow", 2);
  // 添加时清空spuStore
  spuStore.$reset();
};

// 更新SPU
const updateSpu = (row: GetProductSPUItem) => {
  emit("setIsShow", 2);
  // 更新时赋值
  spuStore.spu = row;
};
// 封装公共请求的组件
const getProductSPUList = async () => {
  const { category3Id } = categoryStore;
  const res = await getProductSPUListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id,
  });
  currentPage.value = res.current;
  pageSize.value = res.size;
  total.value = res.total;
  spuList.value = res.records;
};
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 5;
      total.value = 0;
      return;
    }
    getProductSPUList();
  },
  { immediate: true }
);
</script>

<style scoped></style>
